package com.baihan.angus.app.game.busshow.adps;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.BbsResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

public class RecommendAdp extends BaseAdp<BbsResponse.DataX.Data, RecommendAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        BbsResponse.DataX.Data data = getItem(position);

        if (null != data) {
            vh.tvContent.setText(data.getContent());

            ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                    vh.ivPic, data.getPics().get(0).getUrl(), R.mipmap.ic_launcher, 0);

            if (null != data.getUser()) {
                if (!TextUtils.isEmpty(data.getUser().getNickname())) {
                    vh.tvName.setText(data.getUser().getNickname());
                }

                ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                        vh.ivAvatar, data.getUser().getAvatar(), R.mipmap.ic_launcher, 0);
            }
        }
        vh.llRecommendItem.setOnClickListener(view -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onItemClick(position);
            }
        });

        vh.tvPraise.setOnClickListener(view -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onPraiseClick(position);
            }
        });

        if (position % 2 == 0) {
            vh.ivPic.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ComUtil.Dp2Px(BaseApplication.getApplication().getApplicationContext(), 230)));
        } else {
            vh.ivPic.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ComUtil.Dp2Px(BaseApplication.getApplication().getApplicationContext(), 206)));
        }

    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        QMUIRadiusImageView ivAvatar, ivPic;
        TextView tvContent, tvName, tvPraise;
        LinearLayout llRecommendItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.ivPic);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvName = itemView.findViewById(R.id.tvName);
            tvPraise = itemView.findViewById(R.id.tvPraise);
            llRecommendItem = itemView.findViewById(R.id.llRecommendItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onPraiseClick(int position);
    }
}
